package com.power.sifa.correction.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.domain.YesNo;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.correction.api.ICre_work_supFeign;
import com.power.sifa.correction.model.Cre_work_sup;
import com.power.sifa.correction.model.pageInfo.Cre_work_supPageInfo;
import com.power.sifa.correction.service.Crc_personService;
import com.power.sifa.correction.service.Cre_warningService;
import com.power.sifa.correction.service.Cre_workService;
import com.power.sifa.correction.service.Cre_work_supService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 督办任务控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Cre_work_supController", tags = {"督办任务"})
public class Cre_work_supController implements ICre_work_supFeign {

    @Resource
    private Cre_work_supService cre_work_supService;
    @Resource
    private Cre_workService cre_workService;
    @Resource
    private Cre_warningService cre_warningService;
    @Resource
    private Crc_personService crc_personService;

    @Override
    @ApiOperation("查询督办任务分页列表")
    @SaCheckPermission("cre_work_sup_list")
    public ResponseDTO list(Cre_work_supPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Cre_work_sup> page = this.cre_work_supService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Cre_work_sup cre_work_sup : page) {
                cre_work_sup.geneText();
                list.add(cre_work_sup);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber() <= 1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看督办任务")
    @SaCheckPermission("cre_work_sup_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Cre_work_sup cre_work_sup = cre_work_supService.getById(id);
        if (cre_work_sup != null) {
            cre_work_sup.geneText();
        }
        return ResponseDTO.succData(cre_work_sup);
    }

    @Override
    @ApiOperation("删除督办任务")
    @SaCheckPermission("cre_work_sup_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        cre_work_supService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改督办任务")
    @SaCheckPermission("cre_work_sup_save")
    public ResponseDTO save(Cre_work_sup cre_work_sup) {
        Assert.notNull(cre_work_sup, "cre_work_sup不能为空");
        if (null == cre_work_sup.getCreatetime()) {
            cre_work_sup.setCreatetime(new Date());
        }
        // 1. 保存督办对象
        cre_work_supService.saveOrUpdate(cre_work_sup);
        // 2. 关联工作记录督办id
        this.cre_workService.setSupid(cre_work_sup.getWorkid(), cre_work_sup.getId());
        // 3. 修改预警信息督办状态
        this.cre_warningService.updateIssup(cre_work_sup.getWarnid(), YesNo.YES);
        // 4. GZT 给司法所发送短信消息
//        this.crc_personService.sendSupMessage(cre_work_sup.getWarnid(), cre_work_sup.getWorkid());
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value = "批量删除督办任务")
    @SaCheckPermission("cre_work_sup_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        cre_work_supService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO batchSave(List<Cre_work_sup> list) {
        this.cre_work_supService.saveAll(list);
        // 1. 批量更新预警主表状态
        List<String> warnIds = list.stream().map(Cre_work_sup::getWarnid).collect(Collectors.toList());
        this.cre_warningService.updateIssup(warnIds, YesNo.YES);
        // 2. GZT 给司法所发送短信消息
//        for (Cre_work_sup cre_work_sup : list) {
//            this.crc_personService.sendSupMessage(cre_work_sup.getWarnid(), cre_work_sup.getWorkid());
//        }
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value = "导出excel")
    @SaCheckPermission("cre_work_sup_list")
    public void output(HttpServletResponse response, Cre_work_supPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "督办任务";
        String[] heads = {"序号", "预警id", "督办下达人id", "督办下达人姓名", "督办意见", "督办时间", "督办的任务id"};
        String[] fields = {"id", "warnid", "createuserid", "createusername", "remark", "createtime", "workid"};
        Page<Cre_work_sup> page = this.cre_work_supService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value = "导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "督办任务";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号", "预警id", "督办下达人id", "督办下达人姓名", "督办意见", "督办时间", "督办的任务id"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

    @Override
    @ApiOperation(value = "导入excel")
    public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id", "warnid", "createuserid", "createusername", "remark", "createtime", "workid"};
        List<Cre_work_sup> list = (List<Cre_work_sup>) ExcelUtil.Import(fields, file, Cre_work_sup.class);
        if (!CollectionUtils.isEmpty(list)) {
            this.cre_work_supService.saveAll(list);
        }
        return ResponseDTO.succ();
    }

}
